﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace ImageControl.Common
{
    public static class AlgebraExtensions
    {
        public const double Epsilon = 1E-8;

        public static Point Plus(this Point a, Point other)
        {
            return new Point(a.X + other.X, a.Y + other.Y);
        }

        public static Point Minus(this Point a, Point other)
        {
            return new Point(a.X - other.X, a.Y - other.Y);
        }

        public static Point MultiplyBy(this Point a, double number)
        {
            return new Point(a.X * number, a.Y * number);
        }

        public static Point Rotate(this Point a, double degree, Point center = new Point())
        {
            Matrix m = new Matrix();
            m.RotateAt(degree, center.X, center.Y);

            return m.Transform(a);
        }

        public static double MultiplyBy(this Point a, Point other)
        {
            return a.X * other.X + a.Y * other.Y;
        }

        public static double Length(this Point a)
        {
            return Math.Sqrt(a.MultiplyBy(a));
        }

        public static bool IsZero(this Point a)
        {
            return a.Length() < Epsilon;
        }

        public static Point UnitVector(this Point a)
        {
            if (a.IsZero())
            {
                throw new ArgumentException("Cannot make unit point of point with zero length!");
            }

            return a.MultiplyBy(1 / a.Length());
        }

        public static bool IsBetween(this double value, double min, double max)
        {
            return min <= value && value <= max;
        }
    }
}
